package com.rentong.wx.cust.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.rentong.wx.cust.dao.CustFlowApplyDao;
import com.rentong.wx.cust.dao.CustFlowDao;
import com.rentong.wx.cust.dao.CustFlowTypeDao;
import com.rentong.wx.cust.dao.WxCustUserMapper;
import com.rentong.wx.cust.domain.CustFlowApplyEntity;
import com.rentong.wx.cust.domain.CustFlowEntity;
import com.rentong.wx.cust.domain.CustFlowTypeEntity;
import com.rentong.wx.cust.domain.WxCustUser;
import com.rentong.wx.cust.dto.CustFlowApplyVo;
import com.rentong.wx.cust.dto.WxCustUserDto;
import com.rentong.wx.cust.service.CustFlowApplyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;

@Service
public class CustFlowApplyServiceImpl implements CustFlowApplyService {

    @Resource
    private CustFlowApplyDao custFlowApplyDao;

    @Autowired
    private WxCustUserMapper wxCustUserMapper;

    @Autowired
    private CustFlowDao custFlowDao;

    @Autowired
    private CustFlowTypeDao custFlowTypeDao;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public CustFlowApplyEntity getUserFlowApplyInfo(Long custId) {
        CustFlowApplyVo userFlowApplyInfo = custFlowApplyDao.getUserFlowApplyInfo(custId);
        if (ObjectUtil.isNotEmpty(userFlowApplyInfo)) {
            userFlowApplyInfo.setTixianAmount(new BigDecimal(0));
        } else {
            userFlowApplyInfo = custFlowApplyDao.getUserAmount(custId);
        }
        return userFlowApplyInfo;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveFlowApply(CustFlowApplyEntity flowApplyEntity, Long custId) {
        //记录下提现前用户的信息
        WxCustUser wc = wxCustUserMapper.selectByPrimaryKey(custId);

        if (ObjectUtil.isNotEmpty(flowApplyEntity)) {
            if (flowApplyEntity.getTixianAmount().compareTo(new BigDecimal("100")) > 0) {
                //扣减余额
                int result = wxCustUserMapper.deductBalance(custId, flowApplyEntity.getTixianAmount());
                if (result == 0) {
                    return false;
                }
//                wc.setLeijiAmount(wc.getLeijiAmount().multiply(flowApplyEntity.getTixianAmount()));
                WxCustUser wxCustUser = new WxCustUser();
                wxCustUser.setCustId(custId);
                wxCustUser.setLeijiAmount(wc.getLeijiAmount().add(flowApplyEntity.getTixianAmount()));
                wxCustUserMapper.updateByPrimaryKeySelective(wxCustUser);
                //插入cust_flow_apply
                flowApplyEntity.setCustId(custId);
                flowApplyEntity.setState(2);
                flowApplyEntity.setAddTime(new Date());
                custFlowApplyDao.insert(flowApplyEntity);

                //操作流水表
                CustFlowTypeEntity custFlowTypeEntity = custFlowTypeDao.selectByPrimaryKey(8L);
                CustFlowEntity custFlowEntity = new CustFlowEntity();
                custFlowEntity.setCustId(custId);
                custFlowEntity.setType(custFlowTypeEntity.getType());
                custFlowEntity.setFlowTypeName(custFlowTypeEntity.getFlowTypeName());
                custFlowEntity.setFlowTypeId(custFlowTypeEntity.getFlowTypeId());
                custFlowEntity.setFee(flowApplyEntity.getTixianAmount());
                custFlowEntity.setFeeBefore(wc.getBalanceAmount());
                custFlowEntity.setFeeAfter(wc.getBalanceAmount().subtract(flowApplyEntity.getTixianAmount()));
                custFlowEntity.setAddTime(new Date());
                custFlowDao.insertSelective(custFlowEntity);

                return true;
            }
        }
        return false;
    }

}